package cpre556.project2.eclipsePlugin.support;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;

import org.eclipse.core.runtime.Platform;

/**
 * Manage the settings for SSVChecker
 */
public class Settings{
	/**
	 * Last executable location
	 */
	private String last = "";
	
	/**
	 * Hash map of plugin executable locations
	 */
	private HashMap<String, String> plugins;
	
	/**
	 * Location of settings.txt
	 */
	private String settings = "";
	
	/**
	 * List of plug-ins available.
	 */
	private static String[] availablePlugins = {"its4", "rats"};
	
	/**
	 * Constructor
	 */
	public Settings(){
		plugins = new HashMap<String, String>();
		
		settings = Platform.getInstanceLocation().getURL().toString();
		settings = settings.substring(6, settings.length()) + "ssvchecker/settings.txt";
		
		try{
			BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(settings)));
			String line = "";
			
			while((line = reader.readLine()) != null){
				int index = line.indexOf("=");
				
				if(index != -1){	
					saveSetting(line.substring(0, index), line.substring(index + 1), false);
				}
			}
			
			reader.close();
		}
		catch(FileNotFoundException e){
			try{
				String settingDir = settings.substring(0, settings.indexOf("settings.txt"));
				(new File(settingDir)).mkdirs();
				(new File(settings)).createNewFile();
				last = " ";
				saveSettings();
			} 
			catch(IOException e1){
				e1.printStackTrace();
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * Add a location to the hash map and save settings
	 * @param type
	 * @param location
	 */
	public void saveSetting(String type, String location){
		saveSetting(type, location, true);
	}
	
	/**
	 * Add a location to the hash map
	 * @param type
	 * @param location Location of executable
	 * @param save True: Save settings
	 */
	public void saveSetting(String type, String location, boolean save){
		type = type.toLowerCase();
		
		if(type.equals("last")){
			last = location;
		}
		else{
			plugins.put(type, location);
		}
		
		saveSettings();
	}
	
	/**
	 * Write settings to settings.txt
	 */
	public void saveSettings(){
		Iterator<String> iter = plugins.keySet().iterator();
		
		try{
			BufferedWriter writer = new BufferedWriter(new FileWriter(settings));
			
			writer.write("last=" + last + "\n");
			
			while(iter.hasNext()){
				String key = iter.next();
				
				writer.write(key + "=" + plugins.get(key) +  "\n");
			}
			
			writer.close();
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * Save the last executable
	 * @param location
	 */
	public void saveLast(String location){
		saveSetting("last", location);
	}
	
	/**
	 * Get plug-in location
	 * @param key 
	 * @return Plug-in location
	 */
	public String getPlugin(String key){
		key = key.toLowerCase();
		
		if(plugins.containsKey(key)){			
			return plugins.get(key);
		}
		
		return "";
	}
	
	/**
	 * Get the executable name for given key.
	 * @param key
	 * @return Executable name
	 */
	public String getPureExe(String key){
		key = key.toLowerCase();
		
		if(plugins.containsKey(key)){			
			String plugin = plugins.get(key);
			int index = plugin.lastIndexOf("\\");
			
			return plugin.substring(index + 1);
		}
		
		return "";
	}
	
	/**
	 * Return an array of locations.
	 * @return
	 */
	public String[][] getPlugins(){
		Iterator<String> iter = plugins.keySet().iterator();
		String[][] pluginsArray = new String[plugins.size()][2];
		
		int count = 0;
		while(iter.hasNext()){
			String key = iter.next();
			
			pluginsArray[count][0] = key;
			pluginsArray[count][1] = plugins.get(key);
			count++;
		}
		
		return pluginsArray;
	}
	
	/**
	 * Get the last executable location
	 * @return
	 */
	public String getLast(){
		return last;
	}
	
	/**
	 * @return Get available plug-ins.
	 */
	public static String[] getAvailablePlugins(){
		return availablePlugins;
	}
	
	/**
	 * @return Comma separated plug-ins
	 */
	public static String getAvailablePluginsString(){
		String temp = "";
		
		for(int i = 0; i < availablePlugins.length; i++){
			temp += availablePlugins[i].toUpperCase() + ", ";
		}
		
		temp = temp.substring(0, temp.length() - 2);
		
		return temp;
	}
}
